/**
 * Created by admin on 2017/02/04.
 */
function ChartData_WD(init_val_array, auto_data_count, buffer_size)
{
    this.x_data = [];
    this.y_data = [];
    this.y_data_cu = [];
    this.series_name = [];
    this.base_value = [];
    this.line_color = [];
    this.p_data_idx = 0;
    this.e_data_idx = 0;

    this.series_count = init_val_array.length;
    this.buffer_size = buffer_size;
    this.auto_data_count = auto_data_count;

    this.now = +new Date();
    this.now = new Date(+this.now - 1000 * (auto_data_count - 1));
    for (var i = 0; i < this.series_count; i++)
    {
        this.y_data.push([]);
        this.y_data_cu.push(0);
        this.series_name.push(init_val_array[i][0]);
        this.base_value.push(init_val_array[i][1]);
        this.line_color.push(init_val_array[i][2]);
    }

    for (var i = 0; i < this.buffer_size; i++)
        this.addNewDataEmpty();
    for (var i = 0; i < this.auto_data_count; i++)
        this.addNewDataAuto();

    //--------------------------------------------------
    this.chart_wd_option = {
        title: {
            text: ''
        },
        /*
         grid: [
         {x: '60', y: '10'}
         ],*/

        grid: {
            top: 10,
            bottom: 30,
            left: 40,
            right: 10
        },
        tooltip: {
            trigger: 'axis',
            formatter: function (params)
            {
                var dis_str = '';

                if (!isEmptyObject(params[0].value) && params[0].value[0].indexOf('empty') == -1 && params[0].value[1] != null)
                {
                    dis_str = params[0].value[2] + ' ' + params[0].value[0] + '<br/>';
                    for (var idx in params)
                        dis_str += prefixInteger(params[idx].value[3], 5).replace('0', '　') + '&nbsp;:&nbsp;' + params[idx].value[1] + '<br/>';
                }

                return dis_str;
            },
            axisPointer: {
                animation: false
            }
        },
        legend: {
            top: 20,
            left: 70,
            selectedMode: false,
            data: []
        },
        backgroundColor: '#FFFFFF',
        xAxis: {
            type: 'category',
            splitLine: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            boundaryGap: [0, '100%'],
            splitLine: {
                show: true
            }
        },
        series: []
    };

    //--------------------------------------------------
    // this.chart_wd_option.yAxis.min = 500;
    // this.chart_wd_option.yAxis.max = 1800;
    this.chart_wd_option.xAxis.data = this.x_data;
    for (idx in this.series_name)
    {
        var line_style = {};
        if (this.line_color[idx] != '')
            line_style = {normal: {color: this.line_color[idx]}}

        this.chart_wd_option.legend.data.push(this.series_name[idx]);
        this.chart_wd_option.series.push({
            name: this.series_name[idx],
            type: 'line',
            lineStyle: line_style,
            showSymbol: false,
            hoverAnimation: false,
            data: this.y_data[idx]
        });
    }

}
ChartData_WD.prototype = {
    constructor: ChartData_WD,

    getDateStr: function ()
    {
        return [prefixInteger(this.now.getFullYear(), 4), prefixInteger(this.now.getMonth() + 1, 2), prefixInteger(this.now.getDate(), 2)].join('-');
    },

    getTimeStr: function ()
    {
        return [prefixInteger(this.now.getHours(), 2), prefixInteger(this.now.getMinutes(), 2), prefixInteger(this.now.getSeconds(), 2)].join(':');
    },

    randomData: function (x_data, data_idx)
    {
        var y_data;

        this.base_value[data_idx] += (1) * ( Math.random() * 21 - 10);
        y_data = Math.round(this.base_value[data_idx]);

        return this.getYData(x_data, y_data, data_idx);
    },

    getYData: function (x_data, y_data, data_idx)
    {
        return {
            value: [
                x_data,
                y_data,
                this.getDateStr(),
                this.series_name[data_idx]
            ]
        };
    },

    addNewData: function (x_data, y_data_obj_array, shift_flag)
    {
        //this.y_data_cu = [];
        if (shift_flag == true)
            this.x_data.shift();
        this.x_data.push(x_data);
        for (var idx in y_data_obj_array)
        {
            if (shift_flag == true)
                this.y_data[idx].shift();
            this.y_data[idx].push(y_data_obj_array[idx]);
            //this.y_data_cu.push(y_data_obj_array[idx].value[1]);
        }
    },

    updateData: function (x_data, y_data_obj_array, shift_flag)
    {
        for (var idx in y_data_obj_array)
            this.y_data_cu[idx] = y_data_obj_array[idx].value[1];

        if (this.p_data_idx < this.x_data.length)
        {
            this.x_data[this.p_data_idx] = x_data;
            for (var idx in y_data_obj_array)
                this.y_data[idx][this.p_data_idx] = y_data_obj_array[idx];

            this.p_data_idx++;
        }
        else
        {
            this.addNewData(x_data, y_data_obj_array, shift_flag);
        }
    },

    addNewDataAuto: function (shift_flag)
    {
        shift_flag = shift_flag || false;
        this.now = new Date(+this.now + 1000);

        var x_data;
        var y_data_obj_array = [];

        x_data = this.getTimeStr();
        for (var idx in this.y_data)
            y_data_obj_array.push(this.randomData(x_data, idx));
        this.updateData(x_data, y_data_obj_array, shift_flag);
    },

    addNewDataEmpty: function ()
    {
        var x_data;
        var y_data_obj_array = [];

        this.e_data_idx++;
        x_data = (new Array(5 + 1)).join('\n') + 'empty' + this.e_data_idx.toString();
        for (var idx in this.y_data)
            y_data_obj_array.push(this.getYData(x_data, null, idx));

        this.addNewData(x_data, y_data_obj_array, false);
    },

    cuCalc: function ()
    {
        var f_min;
        var f_max;
        var f_avg;
        var f_len;
        var result_array = [];
        var cuTimeStr = '';


        // params[0].value[2] + ' ' + params[0].value[0]

        for (var i = 0; i < this.series_count; i++)
        {
            f_min = 1000000;
            f_max = 0;
            f_avg = 0;
            f_len = 0;
            var array_temp = []
            for (var ii = 0; ii < this.y_data[i].length; ii++)
            {
                var y_data = this.y_data[i][ii].value[1];
                if (y_data != null)
                {
                    if (this.y_data[i][ii].value[2] + ' ' + this.y_data[i][ii].value[0] > cuTimeStr)
                        cuTimeStr = this.y_data[i][ii].value[2] + ' ' + this.y_data[i][ii].value[0];
                    if (y_data < f_min)
                        f_min = y_data;
                    if (y_data > f_max)
                        f_max = y_data;
                    f_avg += y_data;
                    f_len++;
                }
            }
            array_temp.push(f_min);
            array_temp.push(f_max);
            array_temp.push(Math.floor(f_avg / f_len));
            result_array.push(array_temp);
        }
        result_array.push(cuTimeStr);
        result_array.push(f_len);

        return result_array;
    }
}
